#include "carpage.h"
#include "ui_carpage.h"
#include <QMessageBox>

carpage::carpage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::carpage)
{
    ui->setupUi(this);
    updatecost();
}

carpage::~carpage()
{
    delete ui;
}
void carpage::updatecost()
{
    QSqlQuery setcost;
    // 从cost表中检索名为cost'的列，其中type=0
    QString queryStr = "SELECT cost FROM cost WHERE type=0";
    if (!setcost.exec(queryStr)) {
        qDebug() << "Failed to query the cost from database" << setcost.lastError().text();
        return;
    }
    // 检查是否有数据返回
    if (setcost.next()) {
        // 使用列名来获取值，假设列名为'cost'
        QString cost = setcost.value(0).toString();
        ui->lineEdit->setText(cost);
        qDebug() << "Cost value is:" << cost;
    } else {
        qDebug() << "No cost data found for type 0";
        return;
    }
}



void carpage::on_pushButton_clicked()
{
    int cost = ui->lineEdit->text().toInt();
    qDebug() <<  "cost:" << cost ;
    QSqlQuery setcost;
    setcost.prepare("UPDATE cost SET cost=:Cost WHERE type=0");
    setcost.bindValue(":Cost",cost);
    if(!setcost.exec())
    {
        qDebug() << "Failed to updeta the cost from lineEdit" << setcost.lastError().text();
        return;
    }
    QMessageBox::information(this,"提示","更改成功！");

}
